package com.batman.baselibrary.image;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.batman.baselibrary.R;
import com.batman.baselibrary.base.BaseActivity;
import com.batman.baselibrary.utils.BitmapUtil;
import com.batman.baselibrary.widget.Loading;

import java.io.File;

/**
 * 作者：yuhb on 2019-11-10 20:41.
 * 描述：
 */
public class LargeImageActivity extends BaseActivity {

    private static final int REQUEST_CODE_PERMISSION_MULTI = 300;
    private static final int REQUEST_CODE_SETTING = 301;

    SubsamplingScaleImageView mImageView;
    ImageView mImageSave;
    Loading mLoading;
    //    TextView mIntroduceText;
    TextView mTitleTxt;
//    RelativeLayout mIntroduceLayout;

    private String mPath;
    private RequestManager mImgLoader;
//    private Rationale mRationale;
//    private PermissionSetting mSetting;


    public static void show(Context context, String image) {
        Intent intent = new Intent(context, LargeImageActivity.class);
        intent.putExtra("image", image);
        context.startActivity(intent);
    }

    public static void show(Context context, String image, String introduce, String title) {
        Intent intent = new Intent(context, LargeImageActivity.class);
        intent.putExtra("image", image);
        intent.putExtra("introduce", introduce);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }
    @Override
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_large_image;
    }

    @Override
    public void initViews() {

        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        mImageView = findViewById(R.id.imageView);
        mImageSave = findViewById(R.id.iv_save);
        mLoading = findViewById(R.id.loading);
//        mIntroduceText = findViewById(R.id.tv_introduce);
//        mTitleTxt = findViewById(R.id.tv_title);
//        mIntroduceLayout = findViewById(R.id.rl_introduce);
        mImageView.setMaxScale(15);
        mImageView.setZoomEnabled(true);
        mImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPath = getIntent().getStringExtra("image");
        if (mImgLoader == null) {
            mImgLoader = Glide.with(this);
        }
        mImgLoader
                .load(mPath)
                .downloadOnly(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(File resource, Transition<? super File> transition) {
                        if (isDestroyed()) {
                            return;
                        }
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(resource.getPath(), options);
                        int w = options.outWidth;
                        int sw = getScreenWidth(LargeImageActivity.this);
                        float scale = (float) sw / (float) w;
                        mImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
                        mImageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(scale,
                                new PointF(0, 0), BitmapUtil.readPictureDegree(mPath)));
                        mImageSave.setVisibility(View.VISIBLE);
                        mLoading.stop();
                        mLoading.setVisibility(View.GONE);
                    }
                });



    }

    @Override
    public void loadData(Bundle savedInstanceState) {

    }


    @Override
    public void onDestroy() {
        mImgLoader = null;
        super.onDestroy();
    }


    /**
     * 获得屏幕的宽度
     *
     * @param context context
     * @return width
     */
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

}
